﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AviFile; //Avi class by Corinna John

namespace Movement_Trajectory_Analysis.AVI_Convert
{
    public partial class AVIConvert : Form
    {

        OpenFileDialog avidirection;
        FolderBrowserDialog outputPath;

        public AVIConvert()
        {
            InitializeComponent();
        }

        private void AVIConvert_Load(object sender, EventArgs e)
        {
            
        }

        private void chooseDirection_Click(object sender, EventArgs e)
        {
            avidirection = new OpenFileDialog();
            avidirection.Filter = "Video file (*.avi)|*.avi";

            DialogResult resultchooseDirection = avidirection.ShowDialog();
            if (resultchooseDirection == DialogResult.OK)
            {
                aviPath.Text = avidirection.FileName;

                aviInfo.Clear();
                aviInfo.Text = "AVI File description: " + Environment.NewLine + Environment.NewLine;

                AviManager avimanager = new AviManager(avidirection.FileName,true);
                VideoStream vs = avimanager.GetVideoStream();

                aviInfo.Text += "Video stream: " + Environment.NewLine;
                aviInfo.Text += "\tFrame size: " + vs.Width + " x " + vs.Height + Environment.NewLine;
                aviInfo.Text += "\tCount of Frames: " + vs.CountFrames + Environment.NewLine;
                aviInfo.Text += "\tFrame Rate: " + (int)vs.FrameRate + Environment.NewLine;

                TimeSpan ts = new TimeSpan(0, 0, (int)(vs.CountFrames / (int)vs.FrameRate));
                aviInfo.Text += "\tTime: " + ts + Environment.NewLine;

                aviInfo.Text += "\r\nAudio stream:" + Environment.NewLine;

                try
                {
                    AudioStream waveStream = avimanager.GetWaveStream();
                    aviInfo.Text += "\tSamples per Second: " + waveStream.CountSamplesPerSecond.ToString() + Environment.NewLine;
                    aviInfo.Text += "\tBits per Sample: " + waveStream.CountBitsPerSample.ToString() + Environment.NewLine;
                    aviInfo.Text += "\tChannels: " + waveStream.CountChannels.ToString() + Environment.NewLine;
                }
                catch
                {
                    aviInfo.Text += "\t None";
                }
            }  
        }

        private void startConversion_Click(object sender, EventArgs e)
        {
            chooseDirection.Enabled = false;
            aviPath.Enabled = false;
            startConversion.Enabled = false;

            outputPath = new FolderBrowserDialog();
            outputPath.Description = "Choose the frames output direction";
            
            DialogResult resultoutputPath = outputPath.ShowDialog();
            if (resultoutputPath == DialogResult.OK)
            {
                converting.RunWorkerAsync();
            }
            else
            {
                chooseDirection.Enabled = true;
                aviPath.Enabled = true;
                startConversion.Enabled = true;
            }
        }

        private void converting_DoWork(object sender, DoWorkEventArgs e)
        {
            Bitmap bmp;

            try
            {
                AviManager avimanager = new AviManager(avidirection.FileName, true);
                VideoStream videostream = avimanager.GetVideoStream();

                conversionProgressBar.Maximum = videostream.CountFrames;
                videostream.GetFrameOpen();
                for (int i = 0; i < videostream.CountFrames; i++)
                {
                    bmp = videostream.GetBitmap(i);
                    bmp.Save(outputPath.SelectedPath + "\\" + i.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

                    converting.ReportProgress(i);
                }

                videostream.GetFrameClose();
                videostream.Close();
                avimanager.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void converting_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            conversionProgressBar.Value = e.ProgressPercentage;
        }

        private void converting_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            chooseDirection.Enabled = true;
            aviPath.Enabled = true;
            startConversion.Enabled = true;
            conversionProgressBar.Value = 0;

            MessageBox.Show("Conversion completed", "Info", MessageBoxButtons.OK, MessageBoxIcon.Question);
        }

        private void AVIConvert_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (converting.IsBusy)
            {
                converting.CancelAsync();
            }
            
        }
    }
}
